import { BillCodeGenerator } from './../service/tool.service';
import { ShowerUltra } from './../service/Injector.service';
import {
  Inject,
  Controller,
  Get,
  Query,
  ApplicationContext,
  IMidwayContainer,
  Headers,
  Post,
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { Person, UserService } from '../service/user.service';
// import { BillCodeGenerator } from '../service/tool.service';

@Controller('/api')
export class APIController {
  @Inject()
  userService: UserService;
  // @App()
  // app: Application;
  @ApplicationContext()
  applicationContext: IMidwayContainer;
  @Inject()
  ctx: Context;
  @Get('/reqres')
  async reqres(@Headers('user-agent') agent: string) {
    console.log(this.ctx);
    console.log('agent', agent);
    // ctx = request + response
    // ctx 使用语句 获取请求相关信息 @body @param @query @headers 功能相同
    const agent2 = this.ctx.header['user-agent'];
    console.log('agent2', agent2);
    const agent3 = this.ctx.request.header['user-agent'];
    console.log('agent3', agent3);
    // this.ctx.request.body
    // this.ctx.response.body="abc"
  }

  @Post('/ctx2')
  async ctx2() {
    //  {name:"lisi",age:10}
    //  {name:"lisi",age:100}
    const reqBody = this.ctx.request.body;
    console.log(reqBody);
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    reqBody.age = 100;
    // this.ctx.response.body = reqBody;
    this.ctx.body = reqBody;
    return undefined;
  }

  @Get('/applicationContext')
  async appd() {
    console.log(this.applicationContext);
    return 'abc';
  }

  @Inject('zhangsanfeng')
  a: Person = undefined;

  @Get('/person')
  async person() {
    console.log(this.a);
    return true;
  }
  @Get('/get_user')
  async getUser(@Query('uid') uid) {
    const user = await this.userService.getUser({ uid });
    return { success: true, message: 'OK', data: user };
  }

  @Inject('showerUltra')
  showerUltra: ShowerUltra;

  @Get('/99x')
  async x99x() {
    console.log('打印99表');
    this.showerUltra.show();
    return true;
  }

  @Inject()
  billCodeGenerator: BillCodeGenerator;
  @Get('/abc')
  async abc() {
    return this.billCodeGenerator.nextCode();
  }
}
